package com.rfid.project.web;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.PersonWeeklyReport;
import com.rfid.project.service.PersonWeeklyReportService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;
import com.rfid.project.util.DateUtil;

@Controller
public class PersonWeeklyReportController {
	
	private static final Logger logger = LoggerFactory.getLogger(PersonWeeklyReportController.class);
	
	@Autowired
	private PersonWeeklyReportService personWeeklyReportService;
	
	
	@Autowired
	private PeopleService peopleService;
	
	@RequestMapping(value = "/personWeeklyReportmgr")
	public String personWeeklyReportmgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People>peoples = peopleService.findAll();
		model.addAttribute("peoples", peoples);
		return "personWeeklyReportmgr";
	}
	
	@RequestMapping(value = "/weeklyReportQuery")
	public String weeklyReportQuery(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People>peoples = peopleService.findAll();
		model.addAttribute("peoples", peoples);
		return "weeklyReportQuery";
	}
	
	@RequestMapping(value = "/myPersonWeeklyReportmgr")
	public String myPersonWeeklyReportmgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Calendar cal = Calendar.getInstance();
		int index = cal.get(Calendar.DAY_OF_WEEK);
		index--;
		if (index == 0) {
			index = 7;
		}
		int day = 0;
		if(index < 4){
			day = 7;
		}
		cal.add(Calendar.DAY_OF_MONTH, (index - 1) * -1 - day);
		model.addAttribute("start", DateUtil.dateTheDayToString(cal.getTime()));
		cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_WEEK - index - day);
		model.addAttribute("end", DateUtil.dateTheDayToString(cal.getTime()));
		
		return "myPersonWeeklyReportmgr";
	}

	@RequestMapping(value = "/queryPersonWeeklyReport")
	public String queryPersonWeeklyReport(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "workDescription", defaultValue = "") String workDescription,
			@RequestParam(value = "workPlan", defaultValue = "") String workPlan,
			@RequestParam(value = "beginDate", defaultValue = "") Date beginDate,
			@RequestParam(value = "endDate", defaultValue = "") Date endDate,
			@RequestParam(value = "createTimeBegin", defaultValue = "") Date createTimeBegin,
			@RequestParam(value = "createTimeEnd", defaultValue = "") Date createTimeEnd,
			@RequestParam(value = "personId", defaultValue = "-1") Long personId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<PersonWeeklyReport> personWeeklyReports = personWeeklyReportService.findAll(beginDate, endDate, null, null, workDescription, workPlan, createTimeBegin, createTimeEnd, personId, pageNumber, pageSize, sort, order);
				
		JSONObject object = new JSONObject();
		object.put("total", personWeeklyReports.getTotalElements());
		JSONArray models = new JSONArray();
		for (PersonWeeklyReport item : personWeeklyReports) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(PersonWeeklyReport item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("workDescription", item.getWorkDescription());
		mod.put("workPlan", item.getWorkPlan());
		mod.put("beginDate", com.rfid.project.util.DateUtil
				.dateTheDayToString(item.getBeginDate()));	
		mod.put("endDate", com.rfid.project.util.DateUtil
				.dateTheDayToString(item.getEndDate()));
		mod.put("createTime", com.rfid.project.util.DateUtil
				.dateTheDayToString(item.getCreateTime()));
					
		People person_1 = item.getPerson();
		if(null!=person_1){
			mod.put("personId", item.getPerson().getId());
			mod.put("personName", item.getPerson().getName());
		} else {
			mod.put("personId", "-1");
			mod.put("personName", "");				
		}
	}
	
	@RequestMapping(value = "/findOnePersonWeeklyReport")
	public String findOnePersonWeeklyReport(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model){
		PersonWeeklyReport personWeeklyReport = personWeeklyReportService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(personWeeklyReport, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/queryMyPersonWeeklyReport")
	public String queryMyPersonWeeklyReport(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "workDescription", defaultValue = "") String workDescription,
			@RequestParam(value = "workPlan", defaultValue = "") String workPlan,
			@RequestParam(value = "beginDate", defaultValue = "") Date beginDate,
			@RequestParam(value = "endDate", defaultValue = "") Date endDate,
			@RequestParam(value = "createTimeBegin", defaultValue = "") Date createTimeBegin,
			@RequestParam(value = "createTimeEnd", defaultValue = "") Date createTimeEnd,
			@RequestParam(value = "personId", defaultValue = "-1") Long personId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		People currentUser = (People) request.getSession().getAttribute("currentUser");
		Page<PersonWeeklyReport> personWeeklyReports = personWeeklyReportService.findAll(beginDate, endDate, null, null, workDescription, workPlan, createTimeBegin, createTimeEnd, currentUser.getId(), pageNumber, pageSize, sort, order);
				
		JSONObject object = new JSONObject();
		object.put("total", personWeeklyReports.getTotalElements());
		JSONArray models = new JSONArray();
		for (PersonWeeklyReport item : personWeeklyReports) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}

	@RequestMapping(value = "/editPersonWeeklyReport", method = RequestMethod.POST)
	public String editPersonWeeklyReport(HttpServletRequest request,
			HttpServletResponse response, @Valid PersonWeeklyReport personWeeklyReport,
			Model model, RedirectAttributes redirectAttributes) {
		if(null == personWeeklyReport.getPerson() || null == personWeeklyReport.getPerson().getId()){
			personWeeklyReport.setPerson((People)request.getSession().getAttribute("currentUser"));
		}
		personWeeklyReportService.savePersonWeeklyReport(personWeeklyReport);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delPersonWeeklyReport")
	public String delPersonWeeklyReport(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.personWeeklyReportService.deletePersonWeeklyReport(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception{
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
}

